package com.xilihui.xlh.business.activitys;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.xilihui.xlh.BuildConfig;
import com.xilihui.xlh.R;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;

import java.security.Permissions;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by Administrator on 2018/5/17.
 */

public class SplashActivity extends RxAppCompatActivity{

    private AMapLocationClient locationClientContinue = null;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        
        Observable.timer(2, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new Func1<Long, Observable<Boolean>>() {
                    @Override
                    public Observable<Boolean> call(Long aLong) {
                        return new RxPermissions(SplashActivity.this)
                                .request(Manifest.permission.ACCESS_COARSE_LOCATION,
                                        Manifest.permission.ACCESS_FINE_LOCATION,
                                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                        Manifest.permission.READ_EXTERNAL_STORAGE,
                                        Manifest.permission.READ_PHONE_STATE);
                    }
                }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                startActivity(new Intent(SplashActivity.this,HomeActivity.class));
                finish();
            }
        });
        
        startContinueLocation();
        
    }

    /**
     * 启动连续客户端定位
     */
    void startContinueLocation(){
        if(null == locationClientContinue){
            locationClientContinue = new AMapLocationClient(this.getApplicationContext());
        }

        //使用连续的定位方式  默认连续
        AMapLocationClientOption locationClientOption = new AMapLocationClientOption();
        // 地址信息
        locationClientOption.setNeedAddress(true);
        locationClientOption.setInterval(8000);
        locationClientContinue.setLocationOption(locationClientOption);
        locationClientContinue.setLocationListener(locationContinueListener);
        locationClientContinue.startLocation();
    }

    /**
     * 停止连续客户端
     */
    void stopContinueLocation(){
        if(null != locationClientContinue){
            locationClientContinue.stopLocation();
        }
    }

    /**
     * 连续客户端的定位监听
     */
    AMapLocationListener locationContinueListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation location) {
            String lat = location.getLatitude()+"";
            String lng = location.getLongitude()+"";
            SPUtil.put(SplashActivity.this,SPUtil.LAT,lat);
            SPUtil.put(SplashActivity.this,SPUtil.LNG,lng);
            LogUtil.i("location","地址："+
                    (String) SPUtil.get(SplashActivity.this,SPUtil.LAT,"0.0")
                    +(String)SPUtil.get(SplashActivity.this,SPUtil.LNG,"0.0"));
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        stopContinueLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null != locationClientContinue){
            locationClientContinue.onDestroy();
            locationClientContinue = null;
        }
    }
}
